//
//  KRRepairReportListVC.m
//  KEREN
//
//  Created by MW on 2023/4/21.
//

#import "KRRepairReportListVC.h"
#import "KRRepairReportVC.h"
#import "KRRepairReportDetailVC.h"
#import "KROnlienBurglarizedVC.h"
@interface KRRepairReportListVC ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) NSMutableArray *btnArr;
@property (nonatomic, strong) UIView *currentLineView;
@property (nonatomic, strong) KRRepairReportVC *csVC;
@property (nonatomic, strong) KRRepairReportVC *tlVC;
@property (nonatomic, strong) KRRepairReportVC *crVC;
@property (nonatomic, strong) KRRepairReportVC *flVC;
@end

@implementation KRRepairReportListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavView];
    
    UIView *topView = [[UIView alloc] init];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(60 + STATUSBAR_HEIGHT);
        make.height.offset(48);
        make.left.right.offset(0);
    }];
    topView.backgroundColor = Whire_Color;
    //DOTO国际化
    NSArray *titleArr = @[@"全部",@"待接单",@"进行中",@"已完成"];
    for (int i = 0; i < titleArr.count; i++) {
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(DEVICE_WIDTH/4 * i, 0, DEVICE_WIDTH/4, 47)];
        [topView addSubview:btn];
        if(i == 0){
            btn.titleLabel.font = BFont(20);
            [btn setTitleColor:Color_7ACC00 forState:UIControlStateNormal];
            _currentLineView = [[UIView alloc] init];
            [topView addSubview:_currentLineView];
            [_currentLineView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(18);
                make.height.offset(2);
                make.centerX.equalTo(btn);
                make.bottom.offset(-1);
            }];
            _currentLineView.backgroundColor = Color_7ACC00;
        }else{
            btn.titleLabel.font = Font_Medium(16);
            [btn setTitleColor:Color_86898E forState:UIControlStateNormal];
        }
        [btn setTitle:titleArr[i] forState:UIControlStateNormal];
        btn.tag = i;
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.btnArr addObject:btn];
    }
    [UIView setLineViewWithframe:CGRectMake(0, 47, DEVICE_WIDTH, 1) forinView:topView];

    
    
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 108 + STATUSBAR_HEIGHT, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT)];
    [self.view addSubview:_scrollView];
    _csVC = [[KRRepairReportVC alloc] init];
    [_csVC getDeviceRepairLlist:5];
    _tlVC = [[KRRepairReportVC alloc] init];
    _crVC = [[KRRepairReportVC alloc] init];
    _flVC = [[KRRepairReportVC alloc] init];

    [self addChildViewController:_csVC];
    [self addChildViewController:_tlVC];
    [self addChildViewController:_crVC];
    [self addChildViewController:_flVC];

    
    _csVC.view.frame = CGRectMake(0, 0, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT);
    _tlVC.view.frame = CGRectMake(DEVICE_WIDTH, 0, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT);
    _crVC.view.frame = CGRectMake(DEVICE_WIDTH * 2, 0, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT);
    _flVC.view.frame = CGRectMake(DEVICE_WIDTH * 3, 0, DEVICE_WIDTH, DEVICE_HEIGHT - 108 - STATUSBAR_HEIGHT);
    [_scrollView addSubview:_csVC.view];
    [_scrollView addSubview:_tlVC.view];
    [_scrollView addSubview:_crVC.view];
    [_scrollView addSubview:_flVC.view];

    _scrollView.pagingEnabled = YES;
    _scrollView.showsVerticalScrollIndicator = NO;
    _scrollView.showsHorizontalScrollIndicator = NO;
    _scrollView.contentSize = CGSizeMake(DEVICE_WIDTH * 4, 0);
    _scrollView.delegate = self;
//    if(_type > 0){
//        [self updateTopView:_type];
//        [self.scrollView setContentOffset:CGPointMake(DEVICE_WIDTH * _type, 0) animated:NO];
//    }
}

- (void)setNavView{
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, DEVICE_WIDTH, 56 + STATUSBAR_HEIGHT)];
    [self.view addSubview:navView];
    navView.backgroundColor = Whire_Color;
    UIButton *backBtn = [[UIButton alloc] init];
    [navView addSubview:backBtn];
    [backBtn setImage:[UIImage imageNamed:@"main_back"] forState:UIControlStateNormal];
    
    [backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(24);
        make.top.offset(8 + STATUSBAR_HEIGHT);
        make.width.height.offset(40);
    }];
    [backBtn addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *addBtn = [[UIButton alloc] init];
    [navView addSubview:addBtn];
    [addBtn setImage:[UIImage imageNamed:@"devcie_add_icon"] forState:UIControlStateNormal];
    
    [addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-24);
        make.top.offset(8 + STATUSBAR_HEIGHT);
        make.width.height.offset(40);
    }];
    [addBtn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *_titleLbl = [[UILabel alloc] init];
    [navView addSubview:_titleLbl];
    _titleLbl.text = @"报修单列表";
    [_titleLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.centerY.equalTo(backBtn);
    }];
    _titleLbl.font = BFont(20);
    _titleLbl.textColor = Black_Color;
}

- (void)btnClick:(UIButton *)sender{
    [self updateTopView:sender.tag];
    [self.scrollView setContentOffset:CGPointMake(DEVICE_WIDTH * sender.tag, 0) animated:NO];
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    float x = scrollView.contentOffset.x + DEVICE_WIDTH /2;
    int index = x / DEVICE_WIDTH ;
    [self updateTopView:index];
}

- (void)updateTopView:(NSInteger)tag{
    for (UIButton *btn in _btnArr) {
        if(btn.tag == tag){
            btn.titleLabel.font = BFont(20);
            [btn setTitleColor:Color_7ACC00 forState:UIControlStateNormal];
            [_currentLineView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(20);
                make.height.offset(2);
                make.centerX.equalTo(btn);
                make.bottom.offset(-1);
            }];
        }else{
            btn.titleLabel.font = Font_Medium(16);
            [btn setTitleColor:Color_86898E forState:UIControlStateNormal];
        }
    }
    if(tag == 0){
        [_csVC getDeviceRepairLlist:5];
    }else if (tag == 1){
        [_tlVC getDeviceRepairLlist:0];
    }else if(tag == 2){
        [_crVC getDeviceRepairLlist:1];
    }else{
        [_flVC getDeviceRepairLlist:2];
    }
}


- (NSMutableArray *)btnArr{
    if(!_btnArr){
        _btnArr = [NSMutableArray array];
    }
    return _btnArr;
}

- (void)back{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)add{
    if([UserInfoManager sharedInstance].deviceInfoList.count == 0){
        [[KRAlartManager shareInstance] showErrorAlert:@"请绑定车辆后再进行操作"];
        return;
    }

    BOOL is_manager = NO;
    for (DeviceInfo *device in [UserInfoManager sharedInstance].deviceInfoList) {
        if(device.is_manager){
            KROnlienBurglarizedVC *vc = [[KROnlienBurglarizedVC alloc] init];
            vc.isFromRepairReportListVC = YES;
            [self.navigationController pushViewController:vc animated:YES];
            is_manager = YES;
            break;
        }
        is_manager = NO;
    }
    if(!is_manager){
        [[KRAlartManager shareInstance] showAlert:@"该功能仅对车主开放"];
    }
    
}

@end
